package org.raymond.iworks.study.example;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.AES;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

@SpringBootTest
public class AesTest {

    //@Test
    public void test0(){
        AES aes = new AES("CBC", "PKCS7Padding",
                // 密钥，可以自定义
                "0123456789ABHAEQ".getBytes(),
                // iv加盐，按照实际需求添加
                "DYgjCEIMVrj2W9xN".getBytes());
        String plain = "hello world!";
        byte[] encrypted = aes.encrypt(plain);
        String result = new String(aes.decrypt(encrypted), Charset.forName("UTF-8"));
        System.out.println(result);
    }

    @Test
    public void test1() throws IOException {
        String key = "0123456789ABHAEQ";
        String iv = "DYgjCEIMVrj2W9xN";
        byte[] tempKey = key.getBytes(StandardCharsets.UTF_8);

        String hexKey = HexUtil.encodeHexStr(key, StandardCharsets.UTF_8);
        String hexIv = HexUtil.encodeHexStr(iv, StandardCharsets.UTF_8);
        byte[] keyBytes = hexKey.getBytes(StandardCharsets.UTF_8);
        byte[] ivBytes = hexIv.getBytes(StandardCharsets.UTF_8);
        String base64Key = Base64.encode(keyBytes);
        String base64Iv = Base64.encode(ivBytes);

        byte[] keyBytes2 = Base64.decode(base64Key);
        //String hexKey2 = StrUtil.str(keyBytes2, StandardCharsets.UTF_8);
        String hexKey2 = Base64.decodeStr(base64Key);
        byte[] key2 = HexUtil.decodeHex(hexKey2);
        byte[] ivBytes2 = Base64.decode(base64Iv);
        String hexIv2 = StrUtil.str(ivBytes2, StandardCharsets.UTF_8);
        byte[] iv2 = HexUtil.decodeHex(hexIv2);
        AES aes = new AES("CBC", "PKCS7Padding", key2, iv2);


        String plain = "{\"username\":\"yuanyuming\",\"password\":\"123456\"}";
        byte[] encrypted = aes.encrypt(plain);
        String result = new String(aes.decrypt(encrypted), Charset.forName("UTF-8"));
        System.out.println(result);
    }

    @Test
    public void test2(){
        boolean isEncrypt = false;
        String key = "0123456789ABHAEQ";
        String iv = "DYgjCEIMVrj2W9xN";
        AES aes = new AES("CBC", "PKCS7Padding", key.getBytes(StandardCharsets.UTF_8), iv.getBytes(StandardCharsets.UTF_8));

        String plain = "hello world!";
        String hexPlain = HexUtil.encodeHexStr(plain, StandardCharsets.UTF_8);
        String base64Plain = Base64.encode(hexPlain);
        if(isEncrypt) {
            byte[] encrypted = aes.encrypt(plain);
            byte[] decrypted = aes.decrypt(encrypted);
        }else{

        }
        String decodePlain = Base64.decodeStr(base64Plain);
        String hexPlain2 = HexUtil.decodeHexStr(decodePlain);

        //String result = new String(aes.decrypt(encrypted), Charset.forName("UTF-8"));
        System.out.println(hexPlain2);
    }

    @Test
    public void test3(){
        Argon2PasswordEncoder enc = Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
        String s = "123456";
        String pwd = enc.encode(s);
        System.out.println(enc.matches("234", pwd));
    }
}
